package com.beiming.libra.service.impl;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.StatusEnum;
import com.beiming.libra.common.enums.UserProxyEnum;
import com.beiming.libra.common.page.Page;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.ArbitrationUserProxyMapper;
import com.beiming.libra.dao.UserDetailMapper;
import com.beiming.libra.dao.UserMapper;
import com.beiming.libra.domain.ArbitrationUserProxy;
import com.beiming.libra.domain.User;
import com.beiming.libra.domain.UserDetail;
import com.beiming.libra.domain.dto.requestdto.UserDetailRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserProxyAptitudeRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserProxyRequestDTO;
import com.beiming.libra.domain.dto.requestdto.UserRelationRequestDTO;
import com.beiming.libra.domain.dto.responsedto.UserRelationResponseDTO;
import com.beiming.libra.service.UserProxyService;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserProxyServiceImpl implements UserProxyService {

  @Resource
  private ArbitrationUserProxyMapper userProxyMapper;

  @Resource
  private UserMapper userMapper;

  @Resource
  private UserDetailMapper userDetailMapper;

  @Override
  public List<UserRelationResponseDTO> listAllProxy(String userId) {
    Assert.isNotNull(userId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    return userProxyMapper.listAllProxy(userId);
  }

  @Override
  public PageResult<UserRelationResponseDTO> listParty(String userId, int pageSize, int pageNo) {
    UserRelationRequestDTO userRelationRequestDTO = new UserRelationRequestDTO();
    List<UserRelationResponseDTO> list = userProxyMapper.listProxy(userId,
        (pageNo - 1)
            * pageSize, pageSize);
    userRelationRequestDTO.setPageNo(pageNo);
    userRelationRequestDTO.setPageSize(pageSize);
    PageResult<UserRelationResponseDTO> pageResult = new PageResult<UserRelationResponseDTO>();
    pageResult.setList(list);
    int total = userProxyMapper.listProxyCount(userId);
    Page page = new Page();
    page.setPageNo(userRelationRequestDTO.getPageNo());
    page.setPageSize(userRelationRequestDTO.getPageSize());
    page.setTotalRecords(total);
    pageResult.setPage(page);
    return pageResult;
  }

  @Override
  public UserRelationResponseDTO getUserProxyDetail(String id) {
    Assert.isNotNull(id, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    return userProxyMapper.getUserProxyDetail(id);
  }

  @Override
  public void deleteProxy(String id) {
    Assert.isNotNull(id, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    ArbitrationUserProxy arbitrationUserProxy = userProxyMapper.selectByPrimaryKey(id);
    Assert.isNotNull(arbitrationUserProxy, ErrorCode.RESULT_EMPTY, "代理人不存在");
    arbitrationUserProxy.setStatus("1");
    userProxyMapper.updateByPrimaryKey(arbitrationUserProxy);
  }

  @Override
  public void updateProxy(UserRelationRequestDTO userRelationRequestDTO) {
    Assert.isNotNull(userRelationRequestDTO.getId(), ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    ArbitrationUserProxy arbitrationUserProxy = userProxyMapper.selectByPrimaryKey(userRelationRequestDTO.getId());
    Assert.isNotNull(arbitrationUserProxy, ErrorCode.ILLEGAL_PARAMETER, "代理人关系不存在");
    User relationUser = userMapper.selectByPrimaryKey(arbitrationUserProxy.getRelUserId());
    if (!relationUser.getMobilePhone().equals(userRelationRequestDTO.getMobilePhone())) {
      List<User> userRepeat = userMapper
          .selectByMobilePhoneAndType(userRelationRequestDTO.getMobilePhone(),
              userRelationRequestDTO.getUserType());
      Assert.isListEmpty(userRepeat, ErrorCode.MOBILE_EXITS, "该手机号已注册");
    }
    List<UserDetail> userDetailList = userDetailMapper.selectByUserId(arbitrationUserProxy.getRelUserId());
    if (userDetailList.size() > 0) {
      UserDetail userDetail = userDetailList.get(0);
      if (!StringUtils.isEmpty(userRelationRequestDTO.getUserType())) {
        relationUser.setUserType(userRelationRequestDTO.getUserType());
        userDetail.setUserType(userRelationRequestDTO.getUserType());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getMobilePhone())) {
        relationUser.setMobilePhone(userRelationRequestDTO.getMobilePhone());
        userDetail.setMobilePhone(userRelationRequestDTO.getMobilePhone());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getUserName())) {
        userDetail.setUserName(userRelationRequestDTO.getUserName());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getSex())) {
        userDetail.setSex(userRelationRequestDTO.getSex());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getCivilFamily())) {
        userDetail.setCivilFamily(userRelationRequestDTO.getCivilFamily());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getResponsibleUser())) {
        userDetail.setResponsibleUser(userRelationRequestDTO.getResponsibleUser());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getResponsiblePhone())) {
        userDetail.setResponsiblePhone(userRelationRequestDTO.getResponsiblePhone());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getLegalUser())) {
        userDetail.setLegalUser(userRelationRequestDTO.getLegalUser());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getLegalUserJob())) {
        userDetail.setLegalUserJob(userRelationRequestDTO.getLegalUserJob());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getCode())) {
        userDetail.setCode(userRelationRequestDTO.getCode());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getEmail())) {
        userDetail.setEmail(userRelationRequestDTO.getEmail());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getFixedTelephone())) {
        userDetail.setFixedTelephone(userRelationRequestDTO.getFixedTelephone());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getPermanentAddress())) {
        userDetail.setPermanentAddress(userRelationRequestDTO.getPermanentAddress());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getCurrentAddress())) {
        userDetail.setCurrentAddress(userRelationRequestDTO.getCurrentAddress());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getMailAddress())) {
        userDetail.setMailAddress(userRelationRequestDTO.getMailAddress());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getPdetailAddress())) {
        userDetail.setPdetailAddress(userRelationRequestDTO.getPdetailAddress());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getCdetailAddress())) {
        userDetail.setCdetailAddress(userRelationRequestDTO.getCdetailAddress());
      }
      if (!StringUtils.isEmpty(userRelationRequestDTO.getMdetailAddress())) {
        userDetail.setMdetailAddress(userRelationRequestDTO.getMdetailAddress());
      }
      this.userDetailMapper.updateByPrimaryKeySelective(userDetail);
    }
    arbitrationUserProxy.setUserRelationship(userRelationRequestDTO.getUserRelationship());
    arbitrationUserProxy.setAuthType(userRelationRequestDTO.getAuthType());
    arbitrationUserProxy.setLawFirmName(userRelationRequestDTO.getLawFirmName());
    if (UserProxyEnum.LAWYER.getCode().equals(userRelationRequestDTO.getUserRelationship())) {
      Assert.isNotNull(userRelationRequestDTO.getAuthType(), ErrorCode.ILLEGAL_PARAMETER,
          "授权方式不能为空");
      Assert.isNotNull(userRelationRequestDTO.getLawFirmName(), ErrorCode.ILLEGAL_PARAMETER,
          "律师事务所名称不能为空");
      Assert.isNotNull(userRelationRequestDTO.getOfficeUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "证明材料不能为空");
      Assert.isNotNull(userRelationRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "委托书不能为空");
      Assert.isNotNull(userRelationRequestDTO.getLawyerCertificateUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "执业证不能为空");
      arbitrationUserProxy.setOfficeUrl(userRelationRequestDTO.getOfficeUrl());
      arbitrationUserProxy.setProxyUrl(userRelationRequestDTO.getProxyUrl());
      arbitrationUserProxy.setLawyerCertificateUrl(userRelationRequestDTO.getLawyerCertificateUrl());
    } else {
      Assert.isNotNull(userRelationRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "委托书不能为空");
      Assert.isNotNull(userRelationRequestDTO.getRelProofUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "关系证明文件不能为空");
      arbitrationUserProxy.setProxyUrl(userRelationRequestDTO.getProxyUrl());
      arbitrationUserProxy.setRelProofUrl(userRelationRequestDTO.getRelProofUrl());
    }
    this.userMapper.updateByPrimaryKeySelective(relationUser);
    this.userProxyMapper.updateByPrimaryKey(arbitrationUserProxy);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void addProxy(UserProxyRequestDTO userProxyRequestDTO) throws Exception {

    Assert.isNotNull(userProxyRequestDTO.getUserId(), ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    List<User> userRepeat = userMapper
        .selectByMobilePhoneAndType(userProxyRequestDTO.getMobilePhone(),
            userProxyRequestDTO.getUserType());
    Assert.isListEmpty(userRepeat, ErrorCode.MOBILE_EXITS, "该身份类型下手机号已存在");
    //存储代理人基本信息
    User user = new User();
    user.setId(CommonsUtils.get32BitUUID());
    user.setUserType(userProxyRequestDTO.getUserType());
    user.setMobilePhone(userProxyRequestDTO.getMobilePhone());
    user.setStatus(StatusEnum.USED.getCode());
    user.setCreateTime(new Date());
    //存储代理人详细信息
    UserDetail userDetail = BeanConvertUtils.copyBean(userProxyRequestDTO, UserDetail.class);
    userDetail.setId(CommonsUtils.get32BitUUID());
    userDetail.setUserId(user.getId());
    //存储代理人证明材料信息
    ArbitrationUserProxy arbitrationUserProxy = new ArbitrationUserProxy();
    arbitrationUserProxy.setId(CommonsUtils.get32BitUUID());
    arbitrationUserProxy.setUserId(userProxyRequestDTO.getUserId());
    arbitrationUserProxy.setRelUserId(user.getId());
    arbitrationUserProxy.setUserRelationship(userProxyRequestDTO.getUserRelationship());
    arbitrationUserProxy.setAuthType(userProxyRequestDTO.getAuthType());
    arbitrationUserProxy.setLawFirmName(userProxyRequestDTO.getLawFirmName());
    if (UserProxyEnum.LAWYER.getCode().equals(userProxyRequestDTO.getUserRelationship())) {
      Assert.isNotNull(userProxyRequestDTO.getAuthType(), ErrorCode.ILLEGAL_PARAMETER,
          "授权方式不能为空");
      Assert.isNotNull(userProxyRequestDTO.getLawFirmName(), ErrorCode.ILLEGAL_PARAMETER,
          "律师事务所名称不能为空");
      Assert.isNotNull(userProxyRequestDTO.getOfficeUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "证明材料不能为空");
      Assert.isNotNull(userProxyRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "委托书不能为空");
      Assert.isNotNull(userProxyRequestDTO.getLawyerCertificateUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "执业证不能为空");
      arbitrationUserProxy.setOfficeUrl(userProxyRequestDTO.getOfficeUrl());
      arbitrationUserProxy.setProxyUrl(userProxyRequestDTO.getProxyUrl());
      arbitrationUserProxy.setLawyerCertificateUrl(userProxyRequestDTO.getLawyerCertificateUrl());
    } else {
      Assert.isNotNull(userProxyRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "委托书不能为空");
      Assert.isNotNull(userProxyRequestDTO.getRelProofUrl(), ErrorCode.ILLEGAL_PARAMETER,
          "关系证明文件不能为空");
      arbitrationUserProxy.setProxyUrl(userProxyRequestDTO.getProxyUrl());
      arbitrationUserProxy.setRelProofUrl(userProxyRequestDTO.getRelProofUrl());
    }
    arbitrationUserProxy.setStatus(StatusEnum.USED.getCode());
    arbitrationUserProxy.setCreateTime(new Date());
    this.userMapper.insertSelective(user);
    this.userDetailMapper.insertSelective(userDetail);
    this.userProxyMapper.insertSelective(arbitrationUserProxy);
  }

  @Override
  public void addProxyAptitude(UserProxyAptitudeRequestDTO userProxyAptitudeRequestDTO)
      throws Exception {

    Assert.isNotNull(userProxyAptitudeRequestDTO, ErrorCode.ILLEGAL_PARAMETER,
        ValidateMessage.ID_NOT_BLANK);
    for (UserDetailRequestDTO userProxyRequestDTO : userProxyAptitudeRequestDTO
        .getUserDetailRequestDTOList()) {
      List<User> userRepeat = userMapper
          .selectByMobilePhoneAndType(userProxyRequestDTO.getMobilePhone(),
              userProxyRequestDTO.getUserType());
      Assert.isListEmpty(userRepeat, ErrorCode.MOBILE_EXITS, "该身份类型下手机号已存在");
      //存储代理人基本信息
      User user = new User();
      user.setId(CommonsUtils.get32BitUUID());
      user.setUserType(userProxyRequestDTO.getUserType());
      user.setMobilePhone(userProxyRequestDTO.getMobilePhone());
      user.setStatus(StatusEnum.USED.getCode());
      user.setCreateTime(new Date());
      //存储代理人详细信息
      UserDetail userDetail = BeanConvertUtils.copyBean(userProxyRequestDTO, UserDetail.class);
      userDetail.setId(CommonsUtils.get32BitUUID());
      userDetail.setUserId(user.getId());
      //存储代理人证明材料信息
      ArbitrationUserProxy arbitrationUserProxy = new ArbitrationUserProxy();
      arbitrationUserProxy.setId(CommonsUtils.get32BitUUID());
      arbitrationUserProxy.setUserId(user.getId());
      arbitrationUserProxy.setRelUserId(userProxyRequestDTO.getUserId());
      arbitrationUserProxy.setUserRelationship(userProxyAptitudeRequestDTO.getUserRelationship());
      arbitrationUserProxy.setAuthType(userProxyAptitudeRequestDTO.getAuthType());
      arbitrationUserProxy.setLawFirmName(userProxyAptitudeRequestDTO.getLawFirmName());
      if (UserProxyEnum.LAWYER.getCode()
          .equals(userProxyAptitudeRequestDTO.getUserRelationship())) {
        Assert.isNotNull(userProxyAptitudeRequestDTO.getAuthType(), ErrorCode.ILLEGAL_PARAMETER,
            "授权方式不能为空");
        Assert.isNotNull(userProxyAptitudeRequestDTO.getLawFirmName(), ErrorCode.ILLEGAL_PARAMETER,
            "律师事务所名称不能为空");
        Assert.isNotNull(userProxyAptitudeRequestDTO.getOfficeUrl(), ErrorCode.ILLEGAL_PARAMETER,
            "证明材料不能为空");
        Assert.isNotNull(userProxyAptitudeRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
            "委托书不能为空");
        Assert.isNotNull(userProxyAptitudeRequestDTO.getLawyerCertificateUrl(),
            ErrorCode.ILLEGAL_PARAMETER,
            "执业证不能为空");
        arbitrationUserProxy.setOfficeUrl(userProxyAptitudeRequestDTO.getOfficeUrl());
        arbitrationUserProxy.setProxyUrl(userProxyAptitudeRequestDTO.getProxyUrl());
        arbitrationUserProxy
            .setLawyerCertificateUrl(userProxyAptitudeRequestDTO.getLawyerCertificateUrl());
      } else {
        Assert.isNotNull(userProxyAptitudeRequestDTO.getProxyUrl(), ErrorCode.ILLEGAL_PARAMETER,
            "委托书不能为空");
        Assert.isNotNull(userProxyAptitudeRequestDTO.getRelProofUrl(), ErrorCode.ILLEGAL_PARAMETER,
            "关系证明文件不能为空");
        arbitrationUserProxy.setProxyUrl(userProxyAptitudeRequestDTO.getProxyUrl());
        arbitrationUserProxy.setRelProofUrl(userProxyAptitudeRequestDTO.getRelProofUrl());
      }
      arbitrationUserProxy.setStatus(StatusEnum.USED.getCode());
      arbitrationUserProxy.setCreateTime(new Date());
      this.userMapper.insertSelective(user);
      this.userDetailMapper.insertSelective(userDetail);
      this.userProxyMapper.insertSelective(arbitrationUserProxy);
    }
  }
}
